﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;

namespace NetworkingLibrary.Clients.TCP.Java
{
    public class JavaTCPClient : TCPClient
    {
        public JavaTCPClient() : base()
        {
        }
        public JavaTCPClient(Events.MessageReceivedDelegate OnAMessageReceived) : base(OnAMessageReceived)
        {
        }
        public JavaTCPClient(Socket ASocket) 
            : base(ASocket)
        {
        }

        public override bool Stop()
        {
            try
            {
                if (theSocket != null && theSocket.Connected)
                {
                    Terminating = true;

                    Dictionary<string, Messages.Java.JavaDataObject> Objs = new Dictionary<string, Messages.Java.JavaDataObject>();
                    Objs.Add("Disconnect", new Messages.Java.JavaDisconnect());
                    bool OK = Send(new Messages.Java.JavaMessageObject(Objs));
                    System.Threading.Thread.Sleep(100);
                    return OK;
                }
                else
                {
                    return true;
                }
            }
            catch
            {
            }
            return false;
        }
                
        protected override Messages.MessageObject MessageFromBytes(byte[] MessageBytes)
        {
            return new Messages.Java.JavaMessageObject(MessageBytes);
        }
        protected override byte[] MessageToBytes(Messages.MessageObject TheMessage)
        {
            return  ((Messages.Java.JavaMessageObject)TheMessage).ToBytes();
        }
        protected override List<byte[]> GetAllMessages(byte[] BytesRead, out byte[] RemainingBytes)
        {
            List<byte> bytesRead = BytesRead.ToList();
            List<byte[]> RetData = new List<byte[]>();

            byte[] RemBytes = null;
            while (bytesRead.Count > 0)
            {
                int MessageLength = BitConverter.ToInt32(bytesRead.Take(4).ToArray(), 0);
                if (MessageLength > 0)
                {
                    if (bytesRead.Count < MessageLength)
                    {
                        RemBytes = bytesRead.ToArray();
                    }
                    else
                    {
                        bytesRead.RemoveRange(0, 4);
                        RetData.Add(bytesRead.Take(MessageLength - 4).ToArray());
                        bytesRead.RemoveRange(0, MessageLength - 4);
                    }
                }
                else
                {
                    break;
                }
            }

            RemainingBytes = RemBytes;
            return RetData;
        }
        protected override bool IsDisconnect(Messages.MessageObject TheMessage)
        {
            Messages.Java.JavaMessageObject Msg = ((Messages.Java.JavaMessageObject)TheMessage);
            if (Msg.AllObjects.ContainsKey("Disconnect") &&
                Msg.AllObjects["Disconnect"].GetType() == typeof(Messages.Java.JavaDisconnect))
            {
                return true;
            }
            return false;
        }
    }
}
